package org.cycuchy.hp.components.book.gallery {
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Button;
	import mx.core.Application;
	
	import org.cycuchy.hp.app.HPApplication;
	import org.cycuchy.hp.components.FotoCanvas;
	import org.cycuchy.hp.components.book.NumeredPage;
	import org.cycuchy.hp.data.FotoData;
	import org.cycuchy.hp.data.FotosData;
	import org.cycuchy.hp.event.PreloadingImageEvent;

	public class GalleryPage__ extends NumeredPage {
		
		private var loaded:Boolean = false;
		
		private var _data:FotosData;
		private var _pnum:uint;
		
		public function GalleryPage__(data:FotosData, pnum:uint) {
			super();
			this._data = data;
			this._pnum = pnum;
			addEventListener(PreloadingImageEvent.LOAD, load);
		}
		
		override protected function createChildren():void {
			super.createChildren();
//			
//			var b:Button = new Button();
//			b.addEventListener(MouseEvent.CLICK, function():void {
//				(Application.application as HPApplication).showTitleWindow();
//			});
//			addChild(b);
//			var canvas:GradientCanvas = new GradientCanvas();
//			canvas.startColor = side == Page.LEFT ? 0x0898BE : 0x46B3CF; 
//			canvas.endColor = side == Page.LEFT ? 0x46B3CF : 0x0898BE;
//			canvas.angle = 0;
//			canvas.setStyle("horizontalCenter", 0);
//			canvas.setStyle("verticalCenter", 0);
//			canvas.width = this.width - 50;
//			canvas.height = this.height - 50;
//			addChild(canvas);
			
			loadImages();
		}
		
		private function loadImages():void {
			var cols:uint = _data.cols;
			var rows:uint = _data.rows;
			var polaroidWidth:uint = _data.polaroidWidth;
			var polaroidHeight:uint = _data.polaroidHeight;
			var scaleX:Number = _data.scaleX;
			var scaleY:Number = _data.scaleY;
			var verticalGap:uint = _data.verticalGap;
			var horizontalGap:uint = _data.horizontalGap;
			var fotos:ArrayCollection = _data.fotos;
			var fotoC:FotoCanvas;
			var foto:FotoData;
			var n:uint = rows*cols*_pnum;
			var xCenter:uint = width/2 - (cols*polaroidWidth + (cols-1)*horizontalGap)/2;
			var yCenter:uint = height/2 - (rows*polaroidHeight + (rows-1)*verticalGap)/2;
			var count:uint = n;
			for (var row:uint=0; row<rows; row++) {
				for (var col:uint=0; col<cols; col++) {
					if (fotos.length > count) {
						foto = FotoData(fotos.getItemAt(count));
						fotoC = new FotoCanvas(scaleX, scaleY, polaroidWidth, polaroidHeight, foto);
						fotoC.x = xCenter + horizontalGap*col + col*polaroidWidth;
						fotoC.y = yCenter + verticalGap*row + row*polaroidHeight;
						addChild(fotoC);
						count++;
					}
				}
			}						
		}
		
		private function load(event:PreloadingImageEvent):void {
//			trace("GalleryPage::load " + event.index);
			if (!loaded) {
				if (event.index%2 == 0) {
					var i:int = index - event.index;
					if (i == 1 || i == 2) {
						loaded = true;
						dispatch(PreloadingImageEvent.LOAD, event.index);	
					}			
				}
			}
		}
		
		private function dispatch(event:String, index:int):void {
			var children:Array = getChildren();
			for each (var child:DisplayObject in children) {
				if (child is FotoCanvas) {
					child.dispatchEvent(new PreloadingImageEvent(event, index));					
				}
			}
		}
		
	}
	
}